import {
  Group,
  Vector3,
  CatmullRomCurve3,
  MeshBasicMaterial,
  AdditiveBlending,
  Mesh,
  TubeGeometry,
} from "three";
export class PathLine {
  constructor({ time, geoProjection }, options) {
    this.time = time;
    this.geoProjection = geoProjection;
    this.instance = new Group();
    this.run = true;
    let defaultOptions = {
      speed: 0.001,
      texture: null,
      radius: 0.1,
      segments: 32,
      radialSegments: 8,
      data: [],
      renderOrder: 1,
      material: new MeshBasicMaterial({
        color: 0xffffff,
        transparent: true,
        fog: false,
        depthTest: false,
        blending: AdditiveBlending,
      }),
    };
    this.options = Object.assign({}, defaultOptions, options);
    this.init();
  }
  // 初始化
  init() {
    const {
      material,
      texture,
      segments,
      radius,
      radialSegments,
      data,
      speed,
      renderOrder,
    } = this.options;
    this._tickHandler = (delta) => {
      debugger
      if (this.run) {
        texture.offset.x += speed * delta;
      }
    };
    // 计算地图宽度（自适应轮廓线宽度）
    let mapWidth = 1;
    if (this.boundBox && this.boundBox.max && this.boundBox.min) {
      mapWidth = this.boundBox.max.x - this.boundBox.min.x;
    } else if (
      data.length &&
      data[0].geometry &&
      data[0].geometry.coordinates
    ) {
      // 粗略计算所有点的x范围
      let allX = [];
      data.forEach((path) => {
        let coordinates = path.geometry.coordinates;
        if (Array.isArray(coordinates[0][0][0])) {
          // MultiPolygon
          coordinates.forEach((ring) => {
            ring[0].forEach((item) => allX.push(item[0]));
          });
        } else {
          // Polygon
          coordinates.forEach((ring) => {
            ring.forEach((item) => allX.push(item[0]));
          });
        }
      });
      if (allX.length > 1) {
        mapWidth = Math.max(...allX) - Math.min(...allX);
      }
    }
    // 设定自适应半径
    const adaptiveRadius = mapWidth * 0.01;

    data.map((path) => {
      let coordinates = path.geometry.coordinates;
      // 判断是Polygon还是MultiPolygon
      if (Array.isArray(coordinates[0][0][0])) {
        // MultiPolygon
        coordinates.forEach((ring) => {
          let pathPoint = ring[0].map((item) => {
            let [x, y] = this.geoProjection(item);
            return new Vector3(x, -y, 0);
          });
          const curve = new CatmullRomCurve3(pathPoint, true);
          const tubeGeometry = new TubeGeometry(
            curve,
            segments,
            adaptiveRadius,
            radialSegments,
            true
          );
          const mesh = new Mesh(tubeGeometry, material);
          mesh.position.set(0, 0, 0);
          mesh.renderOrder = renderOrder;
          this.instance.add(mesh);
        });
      } else {
        // Polygon
        coordinates.forEach((ring) => {
          let pathPoint = ring.map((item) => {
            let [x, y] = this.geoProjection(item);
            return new Vector3(x, -y, 0);
          });
          const curve = new CatmullRomCurve3(pathPoint, true);
          const tubeGeometry = new TubeGeometry(
            curve,
            segments,
            adaptiveRadius,
            radialSegments,
            true
          );
          const mesh = new Mesh(tubeGeometry, material);
          mesh.position.set(0, 0, 0);
          mesh.renderOrder = renderOrder;
          this.instance.add(mesh);
        });
      }
    });

    this.time.on("tick", this._tickHandler);
  }
destroy() {
  if (this._tickHandler) {
    this.time.off("tick", this._tickHandler);
    this._tickHandler = null;
  }
}
  // 获取实例
  getInstance() {
    return this.instance;
  }
  // 设置父级
  setParent(parent) {
    parent.add(this.instance);
  }
  // 设置隐藏显示
  set visible(bool) {
    this.instance.visible = bool;
    this.run = bool;
  }
}
